﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;
using YArchitech.Revit.RevitExtension;
using HYElectricSetting;

namespace Model
{
	public class HYCableTrayConnect
	{
		public void NewElbowFitting(IList<CableTray> cableTrays)
		{
			try
			{
				if (cableTrays.Count < 2)
				{
					throw new System.ArgumentNullException("CableTray", "is null");
				}
				Document document = cableTrays[0].Document;
				HYCableTray<CableTray> hycableTray = new HYCableTray<CableTray>(cableTrays[0]);
				HYCableTray<CableTray> hycableTray2 = new HYCableTray<CableTray>(cableTrays[1]);
				XYZ xyz = hycableTray.IntersectionPoint(hycableTray2.CableTray);
				if (xyz != null)
				{
					Connector appropriateConnector = hycableTray.GetAppropriateConnector(xyz);
					Connector appropriateConnector2 = hycableTray2.GetAppropriateConnector(xyz);
					Transform coordinateSystem = appropriateConnector.CoordinateSystem;
					Transform coordinateSystem2 = appropriateConnector2.CoordinateSystem;
					if (appropriateConnector == null || appropriateConnector2 == null || appropriateConnector.IsConnected || appropriateConnector2.IsConnected)
					{
						throw new Exception("桥架已经连接或桥架位置不正确。");
					}
					if (hycableTray.IsSamePlan(hycableTray2.CableTray))
					{
						document.Create.NewElbowFitting(appropriateConnector, appropriateConnector2);
					}
					else if (hycableTray.IsXYPlan() && !hycableTray2.IsXYPlan())
					{
						if (coordinateSystem.BasisX.IsAlmostEqualTo(coordinateSystem2.BasisX) || coordinateSystem.BasisX.IsAlmostEqualTo(-coordinateSystem2.BasisX))
						{
							document.Create.NewElbowFitting(appropriateConnector, appropriateConnector2);
						}
						else
						{
							this.NewVerticalElbowFitting(document, hycableTray, hycableTray2);
						}
					}
					else if (hycableTray2.IsXYPlan() && !hycableTray.IsXYPlan())
					{
						if (coordinateSystem.BasisX.IsAlmostEqualTo(coordinateSystem2.BasisX) || coordinateSystem.BasisX.IsAlmostEqualTo(-coordinateSystem2.BasisX))
						{
							document.Create.NewElbowFitting(appropriateConnector, appropriateConnector2);
						}
						else
						{
							this.NewVerticalElbowFitting(document, hycableTray, hycableTray2);
						}
					}
				}
				else
				{
					this.NewVerticalElbowFitting(document, hycableTray, hycableTray2);
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		private void NewVerticalElbowFitting(Document doc, HYCableTray<CableTray> myCustomCableTray, HYCableTray<CableTray> myCustomCableTray1)
		{
			HYCableTray<CableTray> hycableTray = null;
			HYCableTray<CableTray> hycableTray2 = null;
			if (myCustomCableTray.IsXYPlan() && !myCustomCableTray1.IsXYPlan())
			{
				hycableTray = myCustomCableTray;
				hycableTray2 = myCustomCableTray1;
			}
			else if (myCustomCableTray1.IsXYPlan() && !myCustomCableTray.IsXYPlan())
			{
				hycableTray = myCustomCableTray1;
				hycableTray2 = myCustomCableTray;
			}
			if (hycableTray != null && hycableTray2 != null)
			{
				IList<Connector> appropriateConnector = hycableTray.GetAppropriateConnector(hycableTray2.CableTray);
				this.ElbowFittingConnect(doc, hycableTray, hycableTray2, appropriateConnector[0], appropriateConnector[1]);
			}
		}

		private void ElbowFittingConnect(Document doc, HYCableTray<CableTray> horizontalCableTray, HYCableTray<CableTray> verticalCableTray, Connector horizontalConnector, Connector verticalConnector)
		{
			CableTrayType cableTrayType = doc.GetElement(horizontalCableTray.CableTray.GetTypeId()) as CableTrayType;
			Connector otherConnector = this.GetOtherConnector(verticalCableTray, verticalConnector);
			if (this.IsVerticalIntersection(horizontalCableTray, verticalCableTray))
			{
				XYZ lineProject = horizontalCableTray.GetLineProject(verticalCableTray.EndXYZ);
				VerticalCableTrayFittingType verticalCableTrayFittingType;
				if (otherConnector.Origin.Z > horizontalConnector.Origin.Z)
				{
					verticalCableTrayFittingType = this.JudgeElbowType(horizontalConnector, verticalConnector, true);
				}
				else
				{
					verticalCableTrayFittingType = this.JudgeElbowType(horizontalConnector, verticalConnector, false);
				}
				FamilySymbol fittingType = CableTrayFitingSel.GetFittingType(doc, verticalCableTrayFittingType, cableTrayType);
				if (fittingType == null)
				{
					throw new Exception("请先进行桥架设置。");
				}
				Connector otherConnector2 = this.GetOtherConnector(verticalCableTray, verticalConnector);
				XYZ vector = (verticalConnector.Origin - otherConnector2.Origin).Normalize();
				double z = otherConnector2.Origin.Z;
				UnifiedModified.ActivateFamilySymbol(fittingType);
				FamilyInstance familyInstance = doc.Create.NewFamilyInstance(lineProject, fittingType, 0);
				doc.Regenerate();
				this.RotateFitting2(horizontalCableTray, horizontalConnector, familyInstance);
				if (!otherConnector.IsConnected)
				{
					this.RotateTray2(horizontalConnector, verticalConnector, verticalCableTray, familyInstance);
				}
				doc.Regenerate();
				if (verticalCableTrayFittingType == VerticalCableTrayFittingType.LeftDownSideElbow || verticalCableTrayFittingType == VerticalCableTrayFittingType.RightDownSideElbow)
				{
					familyInstance.GetParameter("桥架宽度A").Set(horizontalCableTray.CableTray.Width);
					familyInstance.GetParameter("桥架高度B").Set(horizontalCableTray.CableTray.Height);
				}
				else
				{
					familyInstance.GetParameter("桥架宽度1").Set(horizontalCableTray.CableTray.Width);
					familyInstance.GetParameter("桥架高度").Set(horizontalCableTray.CableTray.Height);
				}
				familyInstance.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM).Set(horizontalCableTray.CableTray.get_Parameter((BuiltInParameter)(-1114132)).AsDouble());
				doc.Regenerate();
				XYZ vector2 = (this.GetOtherConnector(horizontalCableTray, horizontalConnector).Origin - horizontalConnector.Origin).Normalize();
				Line curve = YJKLineEx.YJKGetBound(this.GetConnectorByVector(familyInstance, vector2).Origin, this.GetOtherConnector(horizontalCableTray, horizontalConnector).Origin);
				(horizontalCableTray.CableTray.Location as LocationCurve).Curve = curve;
				doc.Regenerate();
				if (!otherConnector2.IsConnected)
				{
					Connector connectorByVector = this.GetConnectorByVector(familyInstance, vector);
					XYZ origin = connectorByVector.Origin;
					XYZ endPoint = new XYZ(connectorByVector.Origin.X, connectorByVector.Origin.Y, z);
					Line curve2 = YJKLineEx.YJKGetBound(origin, endPoint);
					(verticalCableTray.CableTray.Location as LocationCurve).Curve = curve2;
					doc.Regenerate();
				}
				double width = verticalCableTray.CableTray.Width;
				double height = verticalCableTray.CableTray.Height;
				verticalCableTray.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(horizontalCableTray.CableTray.Width);
				verticalCableTray.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(horizontalCableTray.CableTray.Height);
				doc.Regenerate();
				IList<Connector> appropriateConnector = horizontalCableTray.GetAppropriateConnector(verticalCableTray.CableTray);
				horizontalConnector = appropriateConnector[0];
				verticalConnector = appropriateConnector[1];
				foreach (Connector connector in RevitUtils.GetConduitFittingConnectors(familyInstance))
				{
					if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(XYZ.BasisZ) || connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-XYZ.BasisZ))
					{
						connector.ConnectTo(verticalConnector);
					}
					else
					{
						connector.ConnectTo(horizontalConnector);
					}
				}
				doc.Regenerate();
				verticalCableTray.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(height);
				doc.Regenerate();
			}
		}

		private Connector GetConnectorByVector(FamilyInstance fi, XYZ vector)
		{
			Connector result = null;
			foreach (Connector connector in RevitUtils.GetConduitFittingConnectors(fi))
			{
				if (connector.CoordinateSystem.BasisZ.Normalize().IsAlmostEqualTo(-vector) || connector.CoordinateSystem.BasisZ.Normalize().IsAlmostEqualTo(vector))
				{
					result = connector;
					break;
				}
			}
			return result;
		}

		private VerticalCableTrayFittingType JudgeElbowType(Connector horizontalVector, Connector verticalVector, bool isUp)
		{
			VerticalCableTrayFittingType verticalCableTrayFittingType = VerticalCableTrayFittingType.Undefined;
			XYZ xyz = new XYZ(verticalVector.Origin.X, verticalVector.Origin.Y, 0.0);
			XYZ xyz2 = new XYZ(horizontalVector.Origin.X, horizontalVector.Origin.Y, 0.0);
			XYZ xyz3 = -horizontalVector.CoordinateSystem.BasisZ;
			XYZ xyzpoint = Line.CreateUnbound(xyz2, -xyz3).Project(xyz).XYZPoint;
			XYZ xyz4 = (xyz - xyzpoint).Normalize();
			double num = xyz3.AngleOnPlaneTo(xyz4, XYZ.BasisZ);
			if (Math.Abs(Math.PI*.5 - num) < 0.001)
			{
				if (isUp)
				{
					verticalCableTrayFittingType = VerticalCableTrayFittingType.LeftDownSideElbow;
				}
				else
				{
					verticalCableTrayFittingType = VerticalCableTrayFittingType.LeftUpSideElbow;
				}
			}
			else if (Math.Abs(4.71238898038469 - num) < 0.001)
			{
				if (isUp)
				{
					verticalCableTrayFittingType = VerticalCableTrayFittingType.RightDownSideElbow;
				}
				else
				{
					verticalCableTrayFittingType = VerticalCableTrayFittingType.RightUpSideElbow;
				}
			}
			if (verticalCableTrayFittingType == VerticalCableTrayFittingType.Undefined)
			{
				if (isUp)
				{
					verticalCableTrayFittingType = VerticalCableTrayFittingType.RightDownSideElbow;
				}
				else
				{
					verticalCableTrayFittingType = VerticalCableTrayFittingType.RightUpSideElbow;
				}
			}
			return verticalCableTrayFittingType;
		}

		private void RotateFitting2(HYCableTray<CableTray> horizontalCableTray, Connector horizontalConnector, FamilyInstance fitting)
		{
			Connector connector = null;
			XYZ xyz = (this.GetOtherConnector(horizontalCableTray, horizontalConnector).Origin - horizontalConnector.Origin).Normalize();
			foreach (Connector connector2 in RevitUtils.GetConduitFittingConnectors(fitting))
			{
				if (!connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(XYZ.BasisZ) && !connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(-XYZ.BasisZ))
				{
					connector = connector2;
				}
			}
			if (connector != null)
			{
				double num = connector.CoordinateSystem.BasisZ.AngleOnPlaneTo(xyz, XYZ.BasisZ);
				if (num != 0.0)
				{
					Line line = Line.CreateUnbound((fitting.Location as LocationPoint).Point, XYZ.BasisZ);
					fitting.Location.Rotate(line, num);
				}
			}
		}

		private void RotateTray2(Connector horizontalConnector, Connector verticalConnector, HYCableTray<CableTray> verticalCableTray, FamilyInstance fitting)
		{
			foreach (Connector connector in RevitUtils.GetConduitFittingConnectors(fitting))
			{
				if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0)) || connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0)))
				{
					verticalCableTray.CableTray.Location.Move(connector.Origin - verticalConnector.Origin);
					break;
				}
			}
		}

		public void NewTeeFitting(IList<CableTray> cableTrays)
		{
			SubTransaction subTransaction = null;
			try
			{
				if (cableTrays.Count < 3)
				{
					throw new System.ArgumentNullException("CableTray", "is null");
				}
				Document document = cableTrays[0].Document;
				IList<CableTray> list = new List<CableTray>();
				list = this.OrderCableTrayPosition(cableTrays);
				HYCableTray<CableTray> hycableTray = new HYCableTray<CableTray>(list[0]);
				HYCableTray<CableTray> hycableTray2 = new HYCableTray<CableTray>(list[1]);
				HYCableTray<CableTray> hycableTray3 = new HYCableTray<CableTray>(list[2]);
				HYCableTray<CableTray> hycableTray4 = null;
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				if (hycableTray.IsSamePlan(list[1]) && hycableTray.IsSamePlan(list[2]))
				{
					XYZ xyz = hycableTray.IntersectionPoint(hycableTray2.CableTray);
					if (xyz != null)
					{
						if (hycableTray2.CableTray.UniqueId == hycableTray3.CableTray.UniqueId)
						{
							new List<CableTray>();
							hycableTray4 = new HYCableTray<CableTray>(hycableTray2.Separate(xyz)[1]);
						}
						else if (hycableTray2.IsCableTrayOnLine(hycableTray3.CableTray) || hycableTray2.IsParallel(hycableTray3.CableTray))
						{
							hycableTray4 = new HYCableTray<CableTray>(hycableTray3.CableTray);
						}
						if (hycableTray4 == null)
						{
							throw new Exception("请检查桥架位置是否正确。");
						}
						Connector closeConnector = hycableTray.GetCloseConnector(xyz);
						Connector closeConnector2 = hycableTray2.GetCloseConnector(xyz);
						Connector closeConnector3 = hycableTray4.GetCloseConnector(xyz);
						if (closeConnector == null || closeConnector2 == null || closeConnector3 == null)
						{
							throw new Exception("请检查桥架位置是否正确。");
						}
						if (closeConnector.IsConnected || closeConnector2.IsConnected || closeConnector3.IsConnected)
						{
							throw new Exception("桥架已经连接或桥架位置不正确。");
						}
						document.Create.NewTeeFitting(closeConnector2, closeConnector3, closeConnector);
					}
				}
				else if ((hycableTray.IsXYPlan() && !hycableTray2.IsXYPlan() && !hycableTray3.IsXYPlan()) || (hycableTray2.IsXYPlan() && !hycableTray.IsXYPlan() && !hycableTray3.IsXYPlan()) || (hycableTray3.IsXYPlan() && !hycableTray2.IsXYPlan() && !hycableTray.IsXYPlan()))
				{
					List<HYCableTray<CableTray>> verticalCableTrays = this.GetVerticalCableTrays(new HYCableTray<CableTray>[]
					{
						hycableTray,
						hycableTray2,
						hycableTray3
					});
					if (verticalCableTrays.Count == 2)
					{
						hycableTray2 = verticalCableTrays[0];
						hycableTray3 = verticalCableTrays[1];
						HYCableTray<CableTray> hycableTray5;
						if (hycableTray.CableTray.Id.IntegerValue != hycableTray2.CableTray.Id.IntegerValue && hycableTray.CableTray.Id.IntegerValue != hycableTray3.CableTray.Id.IntegerValue)
						{
							hycableTray5 = hycableTray;
						}
						else if (hycableTray2.CableTray.Id.IntegerValue != hycableTray2.CableTray.Id.IntegerValue && hycableTray2.CableTray.Id.IntegerValue != hycableTray3.CableTray.Id.IntegerValue)
						{
							hycableTray5 = hycableTray2;
						}
						else
						{
							hycableTray5 = hycableTray3;
						}
						hycableTray = hycableTray5;
						XYZ lineProject = hycableTray2.GetLineProject(hycableTray.EndXYZ);
						if (lineProject != null)
						{
							if (hycableTray2.CableTray.UniqueId == hycableTray3.CableTray.UniqueId)
							{
								new List<CableTray>();
								hycableTray4 = new HYCableTray<CableTray>(hycableTray2.Separate(lineProject)[1]);
							}
							else
							{
								hycableTray4 = new HYCableTray<CableTray>(hycableTray3.CableTray);
							}
							if (hycableTray4 != null)
							{
								Connector closeConnector4 = hycableTray.GetCloseConnector(lineProject);
								Connector closeConnector5 = hycableTray2.GetCloseConnector(lineProject);
								Connector closeConnector6 = hycableTray4.GetCloseConnector(lineProject);
								Connector otherConnector = this.GetOtherConnector(hycableTray, closeConnector4);
								XYZ vector = (closeConnector4.Origin - otherConnector.Origin).Normalize();
								XYZ origin = otherConnector.Origin;
								if (closeConnector4 == null || closeConnector4.IsConnected || closeConnector5 == null || closeConnector5.IsConnected || closeConnector6 == null || closeConnector6.IsConnected)
								{
									throw new Exception("桥架已经连接或桥架位置不正确。");
								}
								if (hycableTray2.IsCableTrayOnLine(hycableTray4.CableTray))
								{
									CableTrayType cableTrayType = document.GetElement(hycableTray.CableTray.GetTypeId()) as CableTrayType;
									VerticalCableTrayFittingType verticalCableTrayFittingType = this.JudgeTeeFittingType(closeConnector5, closeConnector4, otherConnector);
									if (verticalCableTrayFittingType != VerticalCableTrayFittingType.Undefined)
									{
										FamilySymbol fittingType = CableTrayFitingSel.GetFittingType(document, verticalCableTrayFittingType, cableTrayType);
										if (fittingType == null)
										{
											throw new Exception("请先进行桥架设置。");
										}
										UnifiedModified.ActivateFamilySymbol(fittingType);
										FamilyInstance familyInstance = document.Create.NewFamilyInstance(lineProject, fittingType, 0);
										document.Regenerate();
										this.RotateFitting3(document, familyInstance, verticalCableTrayFittingType, closeConnector5);
										if (!this.GetOtherConnector(hycableTray2, closeConnector5).IsConnected && !this.GetOtherConnector(hycableTray4, closeConnector6).IsConnected)
										{
											this.MoveTray3(hycableTray, closeConnector4, familyInstance);
										}
										this.TeeFittingConnect1(document, hycableTray2, hycableTray4, closeConnector4, closeConnector5, closeConnector6, familyInstance);
										if (verticalCableTrayFittingType == VerticalCableTrayFittingType.VerticalUpTee)
										{
											familyInstance.GetParameter("桥架宽度1").Set(hycableTray2.CableTray.Width);
											familyInstance.GetParameter("桥架高度").Set(hycableTray2.CableTray.Height);
										}
										else if (verticalCableTrayFittingType == VerticalCableTrayFittingType.VerticalDownTee)
										{
											familyInstance.GetParameter("桥架宽度A").Set(hycableTray2.CableTray.Width);
											familyInstance.GetParameter("桥架高度B").Set(hycableTray2.CableTray.Height);
										}
										document.Regenerate();
										if (!otherConnector.IsConnected)
										{
											XYZ origin2 = this.GetConnectorByVector(familyInstance, vector).Origin;
											XYZ xyz2 = new XYZ(origin.X, origin.Y, origin2.Z);
											Line curve = null;
											if (verticalCableTrayFittingType == VerticalCableTrayFittingType.VerticalUpTee)
											{
												curve = YJKLineEx.YJKGetBound(xyz2, origin2);
											}
											else if (verticalCableTrayFittingType == VerticalCableTrayFittingType.VerticalDownTee)
											{
												curve = YJKLineEx.YJKGetBound(origin2, xyz2);
											}
											(hycableTray.CableTray.Location as LocationCurve).Curve = curve;
											document.Regenerate();
										}
									}
								}
							}
						}
					}
				}
				else
				{
					if ((!hycableTray.IsSamePlan(list[1]) || hycableTray.IsSamePlan(list[2])) && (!hycableTray.IsSamePlan(list[2]) || hycableTray.IsSamePlan(list[1])) && (!hycableTray2.IsSamePlan(list[2]) || hycableTray2.IsSamePlan(list[0])))
					{
						throw new Exception("请检查桥架位置是否正确。");
					}
					HYCableTray<CableTray> verticalCableTray = this.GetVerticalCableTray(new HYCableTray<CableTray>[]
					{
						hycableTray,
						hycableTray2,
						hycableTray3
					});
					if (verticalCableTray.CableTray.Id.IntegerValue == hycableTray.CableTray.Id.IntegerValue)
					{
						hycableTray = verticalCableTray;
					}
					else if (verticalCableTray.CableTray.Id.IntegerValue == hycableTray2.CableTray.Id.IntegerValue)
					{
						hycableTray2 = hycableTray;
						hycableTray = verticalCableTray;
					}
					else
					{
						hycableTray3 = hycableTray;
						hycableTray = verticalCableTray;
					}
					XYZ xyz3 = hycableTray2.GetLineProject(hycableTray.EndXYZ);
					if (xyz3 != null)
					{
						if (hycableTray2.CableTray.UniqueId == hycableTray3.CableTray.UniqueId)
						{
							new List<CableTray>();
							hycableTray4 = new HYCableTray<CableTray>(hycableTray2.Separate(xyz3)[1]);
						}
						else
						{
							hycableTray4 = new HYCableTray<CableTray>(hycableTray3.CableTray);
						}
						if (hycableTray4 == null)
						{
							throw new Exception("请检查桥架位置是否正确。");
						}
						Connector closeConnector7 = hycableTray.GetCloseConnector(xyz3);
						Connector closeConnector8 = hycableTray2.GetCloseConnector(xyz3);
						Connector closeConnector9 = hycableTray4.GetCloseConnector(xyz3);
						Connector otherConnector2 = this.GetOtherConnector(hycableTray, closeConnector7);
						XYZ vector2 = (closeConnector7.Origin - otherConnector2.Origin).Normalize();
						double z = otherConnector2.Origin.Z;
						bool isUp = closeConnector8.Origin.Z < otherConnector2.Origin.Z;
						if (closeConnector7 == null || closeConnector7.IsConnected || closeConnector8 == null || closeConnector8.IsConnected || closeConnector9 == null || closeConnector9.IsConnected)
						{
							throw new Exception("桥架已经连接或桥架位置不正确。");
						}
						XYZ xyz4 = null;
						XYZ xyz5 = null;
						XYZ xyz6 = null;
						XYZ xyz7 = null;
						FamilyInstance familyInstance2 = null;
						VerticalCableTrayFittingType verticalCableTrayFittingType2 = VerticalCableTrayFittingType.Undefined;
						CableTrayType cableTrayType2 = document.GetElement(hycableTray.CableTray.GetTypeId()) as CableTrayType;
						if (hycableTray2.IsCableTrayOnLine(hycableTray4.CableTray))
						{
							verticalCableTrayFittingType2 = this.JudgeTeeFittingType(isUp, closeConnector7, closeConnector8);
							FamilySymbol fittingType2 = CableTrayFitingSel.GetFittingType(document, verticalCableTrayFittingType2, cableTrayType2);
							if (fittingType2 == null)
							{
								throw new Exception("请先进行桥架设置。");
							}
							UnifiedModified.ActivateFamilySymbol(fittingType2);
							familyInstance2 = document.Create.NewFamilyInstance(xyz3, fittingType2, 0);
							document.Regenerate();
							this.RotateFitting3(closeConnector8, familyInstance2, verticalCableTrayFittingType2, closeConnector7);
							if (!otherConnector2.IsConnected)
							{
								this.RotateTray31(hycableTray2, hycableTray, closeConnector7, verticalCableTrayFittingType2, familyInstance2, xyz3);
							}
							document.Regenerate();
							if (!this.IsSameWH(hycableTray2, hycableTray4))
							{
								Transform coordinateSystem = hycableTray2.CustomConnectorStart.Connector.CoordinateSystem;
								xyz4 = coordinateSystem.BasisX;
								xyz5 = coordinateSystem.BasisY;
							}
							this.TeeFittingConnect(document, hycableTray2, hycableTray4, closeConnector7, closeConnector8, closeConnector9, familyInstance2);
						}
						else if (hycableTray2.IsIntersection(hycableTray4.CableTray) && this.IsVerticalIntersection(hycableTray2, hycableTray4))
						{
							xyz3 = hycableTray2.IntersectionPoint(hycableTray4.CableTray);
							if (this.IsSameW(hycableTray, hycableTray2) && this.IsSameW(hycableTray, hycableTray4))
							{
								verticalCableTrayFittingType2 = this.JudgeTeeFittingType(isUp);
								FamilySymbol fittingType2 = CableTrayFitingSel.GetFittingType(document, verticalCableTrayFittingType2, cableTrayType2);
								if (fittingType2 == null)
								{
									throw new Exception("请先进行桥架设置。");
								}
								UnifiedModified.ActivateFamilySymbol(fittingType2);
								familyInstance2 = document.Create.NewFamilyInstance(xyz3, fittingType2, 0);
								document.Regenerate();
								if (!this.GetOtherConnector(hycableTray, closeConnector7).IsConnected)
								{
									this.RotateTray3(hycableTray2, hycableTray, closeConnector7, verticalCableTrayFittingType2, familyInstance2);
								}
								this.RotateFitting3(hycableTray2, closeConnector8, hycableTray4, closeConnector9, familyInstance2, verticalCableTrayFittingType2);
								if (!this.IsSameWH(hycableTray2, hycableTray4))
								{
									Transform coordinateSystem2 = hycableTray.CustomConnectorStart.Connector.CoordinateSystem;
									Transform coordinateSystem3 = hycableTray4.CustomConnectorStart.Connector.CoordinateSystem;
									xyz4 = coordinateSystem2.BasisX;
									xyz5 = coordinateSystem2.BasisY;
									xyz6 = coordinateSystem3.BasisX;
									xyz7 = coordinateSystem3.BasisY;
								}
								this.TeeFittingConnect(document, hycableTray2, hycableTray4, closeConnector7, closeConnector8, closeConnector9, familyInstance2);
							}
						}
						if (verticalCableTrayFittingType2 != VerticalCableTrayFittingType.Undefined)
						{
							if (verticalCableTrayFittingType2 == VerticalCableTrayFittingType.VerticalDownSideTee || verticalCableTrayFittingType2 == VerticalCableTrayFittingType.VerticalUpSideTee)
							{
								familyInstance2.GetParameter("桥架宽度1").Set(hycableTray2.CableTray.Width);
								familyInstance2.GetParameter("桥架宽度3").Set(hycableTray2.CableTray.Width);
								familyInstance2.GetParameter("桥架高度").Set(hycableTray2.CableTray.Height);
							}
							else if (verticalCableTrayFittingType2 == VerticalCableTrayFittingType.VerticalDownTee)
							{
								familyInstance2.GetParameter("桥架宽度A").Set(hycableTray2.CableTray.Width);
								familyInstance2.GetParameter("桥架高度B").Set(hycableTray2.CableTray.Height);
							}
							else if (verticalCableTrayFittingType2 == VerticalCableTrayFittingType.VerticalUpTee || verticalCableTrayFittingType2 == VerticalCableTrayFittingType.VerticalUpCornerTee || verticalCableTrayFittingType2 == VerticalCableTrayFittingType.VerticalDownCornerTee)
							{
								familyInstance2.GetParameter("桥架宽度1").Set(hycableTray2.CableTray.Width);
								familyInstance2.GetParameter("桥架高度").Set(hycableTray2.CableTray.Height);
							}
                            familyInstance2.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM).Set(hycableTray2.CableTray.get_Parameter((BuiltInParameter)(-1114132)).AsDouble());
							document.Regenerate();
							Transform coordinateSystem4 = hycableTray2.CustomConnectorStart.Connector.CoordinateSystem;
							Transform coordinateSystem5 = hycableTray4.CustomConnectorStart.Connector.CoordinateSystem;
							if (xyz4 != null && xyz5 != null && xyz6 == null && xyz7 == null)
							{
								if (coordinateSystem4.BasisX.IsAlmostEqualTo(xyz5) || (coordinateSystem4.BasisX.IsAlmostEqualTo(-xyz5) && coordinateSystem4.BasisY.IsAlmostEqualTo(xyz4)) || coordinateSystem4.BasisY.IsAlmostEqualTo(-xyz4))
								{
									hycableTray2.Rotate90();
								}
							}
							else if (xyz4 != null && xyz5 != null && xyz6 != null && xyz7 != null)
							{
								if (coordinateSystem4.BasisX.IsAlmostEqualTo(xyz5) || (coordinateSystem4.BasisX.IsAlmostEqualTo(-xyz5) && coordinateSystem4.BasisY.IsAlmostEqualTo(xyz4)) || coordinateSystem4.BasisY.IsAlmostEqualTo(-xyz4))
								{
									hycableTray2.Rotate90();
								}
								if (coordinateSystem5.BasisX.IsAlmostEqualTo(xyz6) || (coordinateSystem5.BasisX.IsAlmostEqualTo(-xyz6) && coordinateSystem5.BasisY.IsAlmostEqualTo(xyz6)) || coordinateSystem5.BasisY.IsAlmostEqualTo(-xyz6))
								{
									hycableTray4.Rotate90();
								}
							}
							if (!otherConnector2.IsConnected)
							{
								Connector connectorByVector = this.GetConnectorByVector(familyInstance2, vector2);
								XYZ origin3 = connectorByVector.Origin;
								XYZ xyz8 = new XYZ(connectorByVector.Origin.X, connectorByVector.Origin.Y, z);
								Line curve2;
								if (verticalCableTrayFittingType2 == VerticalCableTrayFittingType.VerticalDownSideTee || verticalCableTrayFittingType2 == VerticalCableTrayFittingType.VerticalDownTee || verticalCableTrayFittingType2 == VerticalCableTrayFittingType.VerticalDownCornerTee)
								{
									curve2 = YJKLineEx.YJKGetBound(origin3, xyz8);
								}
								else
								{
									curve2 = YJKLineEx.YJKGetBound(xyz8, origin3);
								}
								(verticalCableTray.CableTray.Location as LocationCurve).Curve = curve2;
								document.Regenerate();
							}
						}
					}
				}
				document.Regenerate();
				subTransaction.Commit();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
		}

		private VerticalCableTrayFittingType JudgeTeeFittingType(Connector verticalConn, Connector horizontalConn, Connector horizontalOtherConn)
		{
			VerticalCableTrayFittingType result = VerticalCableTrayFittingType.Undefined;
			XYZ xyz = (horizontalOtherConn.Origin - horizontalConn.Origin).Normalize();
			if (xyz.IsAlmostEqualTo(verticalConn.CoordinateSystem.BasisY))
			{
				result = VerticalCableTrayFittingType.VerticalUpTee;
			}
			else if (xyz.IsAlmostEqualTo(-verticalConn.CoordinateSystem.BasisY))
			{
				result = VerticalCableTrayFittingType.VerticalDownTee;
			}
			return result;
		}

		private VerticalCableTrayFittingType JudgeTeeFittingType(bool isUp, Connector verticalConn, Connector horizontalConn)
		{
			VerticalCableTrayFittingType verticalCableTrayFittingType = VerticalCableTrayFittingType.Undefined;
			XYZ basisX = verticalConn.CoordinateSystem.BasisX;
			XYZ basisY = verticalConn.CoordinateSystem.BasisY;
			XYZ basisZ = horizontalConn.CoordinateSystem.BasisZ;
			if (horizontalConn.CoordinateSystem.BasisZ.IsAlmostEqualTo(basisZ) || horizontalConn.CoordinateSystem.BasisZ.IsAlmostEqualTo(-basisZ))
			{
				if (basisX.IsAlmostEqualTo(basisZ) || basisX.IsAlmostEqualTo(-basisZ))
				{
					if (isUp)
					{
						verticalCableTrayFittingType = VerticalCableTrayFittingType.VerticalDownSideTee;
					}
					else
					{
						verticalCableTrayFittingType = VerticalCableTrayFittingType.VerticalUpSideTee;
					}
				}
				else if (basisY.IsAlmostEqualTo(basisZ) || basisY.IsAlmostEqualTo(-basisZ))
				{
					if (isUp)
					{
						verticalCableTrayFittingType = VerticalCableTrayFittingType.VerticalDownTee;
					}
					else
					{
						verticalCableTrayFittingType = VerticalCableTrayFittingType.VerticalUpTee;
					}
				}
			}
			if (verticalCableTrayFittingType == VerticalCableTrayFittingType.Undefined)
			{
				if (isUp)
				{
					verticalCableTrayFittingType = VerticalCableTrayFittingType.VerticalDownSideTee;
				}
				else
				{
					verticalCableTrayFittingType = VerticalCableTrayFittingType.VerticalUpSideTee;
				}
			}
			return verticalCableTrayFittingType;
		}

		private VerticalCableTrayFittingType JudgeTeeFittingType(bool isUp)
		{
			VerticalCableTrayFittingType result;
			if (isUp)
			{
				result = VerticalCableTrayFittingType.VerticalDownCornerTee;
			}
			else
			{
				result = VerticalCableTrayFittingType.VerticalUpCornerTee;
			}
			return result;
		}

		private void TeeFittingConnect(Document doc, HYCableTray<CableTray> horizontalCableTray, HYCableTray<CableTray> horizontalCableTray1, Connector verticalConnector, Connector horizontalConnector, Connector horizontalConnector1, FamilyInstance fitting)
		{
			XYZ lineDirection = this.GetLineDirection(doc, horizontalCableTray, horizontalConnector);
			XYZ lineDirection2 = this.GetLineDirection(doc, horizontalCableTray1, horizontalConnector1);
			foreach (Connector connector in RevitUtils.GetConduitFittingConnectors(fitting))
			{
				if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(lineDirection))
				{
					horizontalConnector.ConnectTo(connector);
				}
				else if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(lineDirection2))
				{
					horizontalConnector1.ConnectTo(connector);
				}
				else
				{
					verticalConnector.ConnectTo(connector);
				}
			}
		}

		private void TeeFittingConnect1(Document doc, HYCableTray<CableTray> verticalCableTray, HYCableTray<CableTray> verticalCableTray1, Connector horizontalConnector, Connector verticalConnector, Connector verticalConnector1, FamilyInstance fitting)
		{
			List<Connector> conduitFittingConnectors = RevitUtils.GetConduitFittingConnectors(fitting);
			XYZ lineDirection = this.GetLineDirection(doc, verticalCableTray, verticalConnector);
			XYZ lineDirection2 = this.GetLineDirection(doc, verticalCableTray1, verticalConnector1);
			foreach (Connector connector in conduitFittingConnectors)
			{
				if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(lineDirection))
				{
					verticalConnector.ConnectTo(connector);
				}
				else if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(lineDirection2))
				{
					verticalConnector1.ConnectTo(connector);
				}
				else
				{
					horizontalConnector.ConnectTo(connector);
				}
			}
		}

		private bool IsSameW(HYCableTray<CableTray> hyCableTray1, HYCableTray<CableTray> hyCableTray2)
		{
			return hyCableTray2.CustomConnectorStart.Connector.Width - hyCableTray1.CustomConnectorStart.Connector.Width < 0.0001;
		}

		public bool IsVerticalIntersection(HYCableTray<CableTray> cableTray, HYCableTray<CableTray> cableTrayOther)
		{
			bool result = false;
			Curve locationCurve = cableTray.GetLocationCurve();
			Curve locationCurve2 = cableTrayOther.GetLocationCurve();
			XYZ xyz = locationCurve.GetEndPoint(1) - locationCurve.GetEndPoint(0);
			XYZ xyz2 = locationCurve2.GetEndPoint(1) - locationCurve2.GetEndPoint(0);
			double num = xyz.AngleTo(xyz2);
			if (Math.PI*.5 - num < 0.001)
			{
				result = true;
			}
			return result;
		}

		private void RotateFitting3(Document doc, FamilyInstance fitting, VerticalCableTrayFittingType fittingType, Connector verticalConnector)
		{
			XYZ point = (fitting.Location as LocationPoint).Point;
			List<Connector> conduitFittingConnectors = RevitUtils.GetConduitFittingConnectors(fitting);
			Connector connector = null;
			foreach (Connector connector2 in conduitFittingConnectors)
			{
				if (connector2.CoordinateSystem.BasisZ.Normalize().IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0)) || connector2.CoordinateSystem.BasisZ.Normalize().IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0)))
				{
					connector = connector2;
					break;
				}
			}
			double num = 0.0;
			if (fittingType == VerticalCableTrayFittingType.VerticalDownTee || fittingType == VerticalCableTrayFittingType.VerticalUpTee)
			{
				num = -verticalConnector.CoordinateSystem.BasisY.AngleOnPlaneTo(connector.CoordinateSystem.BasisY, XYZ.BasisZ);
			}
			if (num != 0.0)
			{
				Line line = Line.CreateUnbound(point, XYZ.BasisZ);
				fitting.Location.Rotate(line, num);
			}
			doc.Regenerate();
			foreach (Connector connector3 in RevitUtils.GetConduitFittingConnectors(fitting))
			{
				if (connector3.CoordinateSystem.BasisZ.Normalize().IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0)) || connector3.CoordinateSystem.BasisZ.Normalize().IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0)))
				{
					connector = connector3;
				}
			}
			Line line2 = Line.CreateBound(point, point + connector.CoordinateSystem.BasisX * 5.0);
			if (fittingType == VerticalCableTrayFittingType.VerticalDownTee)
			{
				ElementTransformUtils.RotateElement(doc, fitting.Id, line2, Math.PI*.5);
				return;
			}
			if (fittingType == VerticalCableTrayFittingType.VerticalUpTee)
			{
				ElementTransformUtils.RotateElement(doc, fitting.Id, line2, -Math.PI*.5);
			}
		}

		private void RotateFitting3(Connector connector, FamilyInstance fitting, VerticalCableTrayFittingType fittingType, Connector verticalConnector)
		{
			Transform coordinateSystem = connector.CoordinateSystem;
			Connector connector2 = null;
			List<Connector> conduitFittingConnectors = RevitUtils.GetConduitFittingConnectors(fitting);
			foreach (Connector connector3 in conduitFittingConnectors)
			{
				if (connector3.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0)) || connector3.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0)))
				{
					connector2 = connector3;
					break;
				}
			}
			double num = 0.0;
			Transform coordinateSystem2 = connector2.CoordinateSystem;
			if (fittingType == VerticalCableTrayFittingType.VerticalDownTee || fittingType == VerticalCableTrayFittingType.VerticalUpTee)
			{
				num = coordinateSystem2.BasisX.AngleOnPlaneTo(coordinateSystem.BasisX, XYZ.BasisZ);
			}
			else if (fittingType == VerticalCableTrayFittingType.VerticalDownSideTee || fittingType == VerticalCableTrayFittingType.VerticalUpSideTee)
			{
				num = coordinateSystem2.BasisX.AngleOnPlaneTo(coordinateSystem.BasisZ, XYZ.BasisZ);
			}
			if (num != 0.0)
			{
				Line line = Line.CreateUnbound((fitting.Location as LocationPoint).Point, XYZ.BasisZ);
				fitting.Location.Rotate(line, num);
			}
			if (fittingType == VerticalCableTrayFittingType.VerticalDownTee || fittingType == VerticalCableTrayFittingType.VerticalUpTee)
			{
				foreach (Connector connector4 in conduitFittingConnectors)
				{
					if (connector4.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0)) || connector4.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0)))
					{
						num = connector4.CoordinateSystem.BasisY.AngleOnPlaneTo(verticalConnector.CoordinateSystem.BasisY, XYZ.BasisZ);
						if (num != 0.0)
						{
							Line line2 = Line.CreateUnbound((fitting.Location as LocationPoint).Point, XYZ.BasisZ);
							fitting.Location.Rotate(line2, Math.PI);
						}
					}
				}
			}
		}

		private void RotateTray3(HYCableTray<CableTray> hyCableTray1, HYCableTray<CableTray> hyCableTray2, Connector verticalConnector, VerticalCableTrayFittingType fittingType, FamilyInstance fitting)
		{
			Transform coordinateSystem = hyCableTray1.CustomConnectorStart.Connector.CoordinateSystem;
			Transform coordinateSystem2 = verticalConnector.CoordinateSystem;
			double num = 0.0;
			if (fittingType == VerticalCableTrayFittingType.VerticalDownTee || fittingType == VerticalCableTrayFittingType.VerticalUpTee)
			{
				num = coordinateSystem2.BasisX.AngleOnPlaneTo(coordinateSystem.BasisX, XYZ.BasisZ);
			}
			else if (fittingType == VerticalCableTrayFittingType.VerticalDownSideTee || fittingType == VerticalCableTrayFittingType.VerticalUpSideTee)
			{
				num = coordinateSystem2.BasisX.AngleOnPlaneTo(coordinateSystem.BasisZ, XYZ.BasisZ);
			}
			else if (fittingType == VerticalCableTrayFittingType.VerticalUpCornerTee || fittingType == VerticalCableTrayFittingType.VerticalDownCornerTee)
			{
				num = coordinateSystem2.BasisX.AngleOnPlaneTo(coordinateSystem.BasisX, XYZ.BasisZ);
			}
			else if (fittingType == VerticalCableTrayFittingType.VerticalUpCornerCross)
			{
				num = coordinateSystem2.BasisX.AngleOnPlaneTo(coordinateSystem.BasisX, XYZ.BasisZ);
			}
			Line line = Line.CreateUnbound(hyCableTray2.StartXYZ, XYZ.BasisZ);
			if (num != 0.0 && num != Math.PI)
			{
				hyCableTray2.CableTray.Location.Rotate(line, num);
			}
			foreach (Connector connector in RevitUtils.GetConduitFittingConnectors(fitting))
			{
				if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0)) || connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0)))
				{
					hyCableTray2.CableTray.Location.Move(connector.Origin - verticalConnector.Origin);
					if (verticalConnector.CoordinateSystem.BasisY.IsAlmostEqualTo(-connector.CoordinateSystem.BasisY))
					{
						hyCableTray2.CableTray.Location.Rotate(line, Math.PI);
						break;
					}
					break;
				}
			}
		}

		private void MoveTray3(HYCableTray<CableTray> hyCableTray1, Connector horizontalConnector, FamilyInstance fitting)
		{
			foreach (Connector connector in RevitUtils.GetConduitFittingConnectors(fitting))
			{
				if (!connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0)) && !connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0)))
				{
					hyCableTray1.CableTray.Location.Move(connector.Origin - horizontalConnector.Origin);
					break;
				}
			}
		}

		private void RotateTray31(HYCableTray<CableTray> horizontalCableTray, HYCableTray<CableTray> verticalCableTray, Connector verticalConnector, VerticalCableTrayFittingType fittingType, FamilyInstance fitting, XYZ intersectionPoint)
		{
			Transform coordinateSystem = horizontalCableTray.CustomConnectorStart.Connector.CoordinateSystem;
			Transform coordinateSystem2 = verticalConnector.CoordinateSystem;
			double num = 0.0;
			if (fittingType == VerticalCableTrayFittingType.VerticalDownTee || fittingType == VerticalCableTrayFittingType.VerticalUpTee)
			{
				num = coordinateSystem2.BasisX.AngleOnPlaneTo(coordinateSystem.BasisX, XYZ.BasisZ);
			}
			else if (fittingType == VerticalCableTrayFittingType.VerticalDownSideTee || fittingType == VerticalCableTrayFittingType.VerticalUpSideTee)
			{
				num = coordinateSystem2.BasisX.AngleOnPlaneTo(coordinateSystem.BasisZ, XYZ.BasisZ);
			}
			else if (fittingType == VerticalCableTrayFittingType.VerticalUpCornerTee)
			{
				num = coordinateSystem2.BasisX.AngleOnPlaneTo(coordinateSystem.BasisX, XYZ.BasisZ);
			}
			else if (fittingType == VerticalCableTrayFittingType.VerticalUpCornerCross)
			{
				num = coordinateSystem2.BasisX.AngleOnPlaneTo(coordinateSystem.BasisX, XYZ.BasisZ);
			}
			if ((fittingType == VerticalCableTrayFittingType.VerticalDownSideTee || fittingType == VerticalCableTrayFittingType.VerticalUpSideTee) && num >= Math.PI)
			{
				num -= Math.PI;
			}
			Line line = Line.CreateUnbound(verticalCableTray.StartXYZ, XYZ.BasisZ);
			if (num != 0.0)
			{
				verticalCableTray.CableTray.Location.Rotate(line, num);
			}
			foreach (Connector connector in RevitUtils.GetConduitFittingConnectors(fitting))
			{
				if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0)) || connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0)))
				{
					verticalCableTray.CableTray.Location.Move(connector.Origin - verticalConnector.Origin);
					if (verticalConnector.CoordinateSystem.BasisY.IsAlmostEqualTo(-connector.CoordinateSystem.BasisY))
					{
						verticalCableTray.CableTray.Location.Rotate(line, Math.PI);
						break;
					}
					break;
				}
			}
		}

		private void RotateFitting3(HYCableTray<CableTray> horizontalCableTray, Connector horizontalConnector, HYCableTray<CableTray> horizontalCableTray1, Connector horizontalConnector1, FamilyInstance fitting, VerticalCableTrayFittingType fittingType)
		{
			XYZ xyz = null;
			XYZ xyz2 = null;
			XYZ xyz3 = null;
			if (fittingType == VerticalCableTrayFittingType.VerticalUpCornerTee)
			{
				this.GetFittingVectors(fitting, ref xyz, ref xyz2, ref xyz3);
			}
			else
			{
				this.GetFittingVectors1(fitting, ref xyz, ref xyz2, ref xyz3);
			}
			XYZ xyz4 = (this.GetOtherConnector(horizontalCableTray, horizontalConnector).Origin - horizontalConnector.Origin).Normalize();
			XYZ xyz5 = (this.GetOtherConnector(horizontalCableTray1, horizontalConnector1).Origin - horizontalConnector1.Origin).Normalize();
			double num = xyz2.Normalize().AngleTo(xyz4);
			Line line = Line.CreateUnbound(xyz, XYZ.BasisZ);
			fitting.Location.Rotate(line, num);
			if (fittingType == VerticalCableTrayFittingType.VerticalUpCornerTee)
			{
				this.GetFittingVectors(fitting, ref xyz, ref xyz2, ref xyz3);
			}
			else
			{
				this.GetFittingVectors1(fitting, ref xyz, ref xyz2, ref xyz3);
			}
			num = xyz3.Normalize().AngleTo(xyz5.Normalize());
			if (num != 0.0)
			{
				for (int i = 0; i < 4; i++)
				{
					line = Line.CreateUnbound(xyz, XYZ.BasisZ);
					fitting.Location.Rotate(line, Math.PI*.5);
					if (fittingType == VerticalCableTrayFittingType.VerticalUpCornerTee)
					{
						this.GetFittingVectors(fitting, ref xyz, ref xyz2, ref xyz3);
					}
					else
					{
						this.GetFittingVectors1(fitting, ref xyz, ref xyz2, ref xyz3);
					}
					double num2 = xyz2.Normalize().AngleTo(xyz4);
					double num3 = xyz2.Normalize().AngleTo(xyz5);
					double num4 = xyz3.Normalize().AngleTo(xyz5);
					double num5 = xyz3.Normalize().AngleTo(xyz4);
					if ((num2 < 0.1 && num4 < 0.1) || (num3 < 0.1 && num5 < 0.1))
					{
						break;
					}
				}
			}
		}

		private void RotateFitting4(HYCableTray<CableTray> hyCableTray1, Connector connector1, Connector connector, FamilyInstance fitting)
		{
			Connector connector2 = null;
			List<Connector> conduitFittingConnectors = RevitUtils.GetConduitFittingConnectors(fitting);
			foreach (Connector connector3 in conduitFittingConnectors)
			{
				if (connector3.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0)) || connector3.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0)))
				{
					connector2 = connector3;
					break;
				}
			}
			Transform coordinateSystem = connector.CoordinateSystem;
			Transform coordinateSystem2 = connector2.CoordinateSystem;
			double num = coordinateSystem2.BasisX.AngleOnPlaneTo(coordinateSystem.BasisZ, XYZ.BasisZ);
			if (num != 0.0)
			{
				Line line = Line.CreateUnbound(connector2.Origin, XYZ.BasisZ);
				fitting.Location.Rotate(line, num);
			}
			coordinateSystem2 = connector2.CoordinateSystem;
			Connector connector4 = null;
			conduitFittingConnectors.Remove(connector2);
			foreach (Connector connector5 in conduitFittingConnectors)
			{
				Transform coordinateSystem3 = connector5.CoordinateSystem;
				if (coordinateSystem2.BasisX.IsAlmostEqualTo(coordinateSystem3.BasisX) || coordinateSystem2.BasisX.IsAlmostEqualTo(-coordinateSystem3.BasisX))
				{
					connector4 = connector5;
					break;
				}
			}
			if (connector4 != null)
			{
				coordinateSystem2 = connector4.CoordinateSystem;
				if ((this.GetOtherConnector(hyCableTray1, connector1).Origin - connector1.Origin).Normalize().IsAlmostEqualTo(-coordinateSystem2.BasisZ))
				{
					Line line2 = Line.CreateUnbound(connector2.Origin, XYZ.BasisZ);
					fitting.Location.Rotate(line2, Math.PI);
				}
			}
		}

		private void GetFittingVectors(FamilyInstance fitting, ref XYZ conn, ref XYZ fittingVector, ref XYZ fittingVector1)
		{
			int index = 0;
			List<Connector> conduitFittingConnectors = RevitUtils.GetConduitFittingConnectors(fitting);
			for (int i = 0; i < conduitFittingConnectors.Count; i++)
			{
				if (conduitFittingConnectors[i].CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0)) || conduitFittingConnectors[i].CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0)))
				{
					index = i;
					break;
				}
			}
			conn = conduitFittingConnectors[index].Origin;
			conduitFittingConnectors.RemoveAt(index);
			fittingVector = conduitFittingConnectors[0].Origin - new XYZ(conn.X, conn.Y, conduitFittingConnectors[0].Origin.Z);
			fittingVector1 = conduitFittingConnectors[1].Origin - new XYZ(conn.X, conn.Y, conduitFittingConnectors[1].Origin.Z);
		}

		private void GetFittingVectors1(FamilyInstance fitting, ref XYZ conn, ref XYZ fittingVector, ref XYZ fittingVector1)
		{
			Connector connector = null;
			List<Connector> conduitFittingConnectors = RevitUtils.GetConduitFittingConnectors(fitting);
			for (int i = 0; i < conduitFittingConnectors.Count; i++)
			{
				if (conduitFittingConnectors[i].CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0)))
				{
					connector = conduitFittingConnectors[i];
					break;
				}
			}
			conduitFittingConnectors.Remove(connector);
			Connector connector2 = null;
			for (int j = 0; j < conduitFittingConnectors.Count; j++)
			{
				if ((conduitFittingConnectors[j].Origin - new XYZ(connector.Origin.X, connector.Origin.Y, conduitFittingConnectors[j].Origin.Z)).Normalize().IsAlmostEqualTo(conduitFittingConnectors[j].CoordinateSystem.BasisZ))
				{
					connector2 = conduitFittingConnectors[j];
					break;
				}
			}
			conduitFittingConnectors.Remove(connector2);
			XYZ xyzpoint = Line.CreateBound(connector2.Origin, new XYZ(connector.Origin.X, connector.Origin.Y, connector2.Origin.Z)).Project(conduitFittingConnectors[0].Origin).XYZPoint;
			conn = xyzpoint;
			fittingVector = connector2.Origin - new XYZ(conn.X, conn.Y, connector2.Origin.Z);
			fittingVector1 = conduitFittingConnectors[0].Origin - new XYZ(conn.X, conn.Y, conduitFittingConnectors[0].Origin.Z);
		}

		private XYZ GetLineDirection(Document doc, HYCableTray<CableTray> hyct, Connector connector)
		{
			XYZ origin = this.GetOtherConnector(hyct, connector).Origin;
			return YJKLineEx.YJKGetBound(connector.Origin, origin).Direction.Normalize();
		}

		private Connector GetOtherConnector(HYCableTray<CableTray> hyct, Connector connector)
		{
			Connector connector2;
			if (!hyct.CustomConnectorStart.Connector.Origin.IsAlmostEqualTo(connector.Origin))
			{
				connector2 = hyct.CustomConnectorStart.Connector;
			}
			else
			{
				connector2 = hyct.CustomConnectorEnd.Connector;
			}
			return connector2;
		}

		public void NewCrossFitting(IList<CableTray> cableTrays)
		{
			SubTransaction subTransaction = null;
			Document document = null;
			try
			{
				if (cableTrays.Count < 4)
				{
					throw new System.ArgumentNullException("CableTray", "is null");
				}
				document = cableTrays[0].Document;
				IList<CableTray> list = new List<CableTray>();
				list = this.OrderCableTrayPosition(cableTrays);
				HYCableTray<CableTray> hycableTray = new HYCableTray<CableTray>(list[0]);
				HYCableTray<CableTray> hycableTray2 = new HYCableTray<CableTray>(list[1]);
				HYCableTray<CableTray> hycableTray3 = new HYCableTray<CableTray>(list[2]);
				HYCableTray<CableTray> hycableTray4 = new HYCableTray<CableTray>(list[3]);
				HYCableTray<CableTray> hycableTray5 = null;
				HYCableTray<CableTray> hycableTray6 = null;
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				if (hycableTray.IsSamePlan(list[1]) && hycableTray3.IsSamePlan(list[3]) && hycableTray3.IsSamePlan(list[1]))
				{
					try
					{
						XYZ xyz = hycableTray.IntersectionPoint(hycableTray3.CableTray);
						if (xyz != null)
						{
							if (hycableTray.CableTray.UniqueId == hycableTray2.CableTray.UniqueId)
							{
								new List<CableTray>();
								hycableTray5 = new HYCableTray<CableTray>(hycableTray.Separate(xyz)[1]);
							}
							else if (hycableTray.IsCableTrayOnLine(hycableTray2.CableTray) || hycableTray.IsParallel(hycableTray2.CableTray))
							{
								hycableTray5 = new HYCableTray<CableTray>(hycableTray2.CableTray);
							}
							if (hycableTray3.CableTray.UniqueId == hycableTray4.CableTray.UniqueId)
							{
								new List<CableTray>();
								hycableTray6 = new HYCableTray<CableTray>(hycableTray3.Separate(xyz)[1]);
							}
							else if (hycableTray3.IsCableTrayOnLine(hycableTray4.CableTray) || hycableTray3.IsParallel(hycableTray4.CableTray))
							{
								hycableTray6 = new HYCableTray<CableTray>(hycableTray4.CableTray);
							}
							if (hycableTray5 == null || hycableTray6 == null)
							{
								throw new Exception("请检查桥架位置是否正确。");
							}
							Connector appropriateConnector = hycableTray.GetAppropriateConnector(xyz);
							Connector appropriateConnector2 = hycableTray5.GetAppropriateConnector(xyz);
							Connector appropriateConnector3 = hycableTray3.GetAppropriateConnector(xyz);
							Connector appropriateConnector4 = hycableTray6.GetAppropriateConnector(xyz);
							if (appropriateConnector == null || appropriateConnector.IsConnected || appropriateConnector2 == null || appropriateConnector2.IsConnected || appropriateConnector3 == null || appropriateConnector3.IsConnected || appropriateConnector4 == null || appropriateConnector4.IsConnected)
							{
								throw new Exception("桥架已经连接或桥架位置不正确。");
							}
							document.Create.NewCrossFitting(appropriateConnector, appropriateConnector2, appropriateConnector3, appropriateConnector4);
						}
						goto IL_729;
					}
					catch (Exception)
					{
						throw new Exception("桥架已经连接或桥架位置不正确。");
					}
				}
				if (!hycableTray.IsCableTrayOnLine(list[1]) && !hycableTray.IsCableTrayOnLine(list[2]) && !hycableTray.IsCableTrayOnLine(list[3]) && !hycableTray2.IsCableTrayOnLine(list[2]) && !hycableTray2.IsCableTrayOnLine(list[3]) && !hycableTray3.IsCableTrayOnLine(list[3]))
				{
					throw new Exception("请检查桥架位置是否正确。");
				}
				HYCableTray<CableTray> verticalCableTray = this.GetVerticalCableTray(new HYCableTray<CableTray>[]
				{
					hycableTray,
					hycableTray2,
					hycableTray3,
					hycableTray4
				});
				hycableTray = verticalCableTray;
				cableTrays.Remove(verticalCableTray.CableTray);
				list = this.OrderCableTrayPosition(cableTrays);
				hycableTray2 = new HYCableTray<CableTray>(list[0]);
				hycableTray3 = new HYCableTray<CableTray>(list[1]);
				hycableTray4 = new HYCableTray<CableTray>(list[2]);
				hycableTray5 = null;
				if (hycableTray.IsSamePlan(hycableTray2.CableTray) || !hycableTray2.IsSamePlan(hycableTray3.CableTray) || !hycableTray3.IsSamePlan(hycableTray4.CableTray))
				{
					throw new Exception("请检查桥架位置是否正确。");
				}
				XYZ xyz2 = hycableTray2.IntersectionPoint(hycableTray3.CableTray);
				if (xyz2 != null)
				{
					if (hycableTray3.CableTray.UniqueId == hycableTray4.CableTray.UniqueId)
					{
						new List<CableTray>();
						hycableTray5 = new HYCableTray<CableTray>(hycableTray3.Separate(xyz2)[1]);
					}
					else if (hycableTray3.IsCableTrayOnLine(hycableTray4.CableTray) || hycableTray3.IsParallel(hycableTray4.CableTray))
					{
						hycableTray5 = new HYCableTray<CableTray>(hycableTray4.CableTray);
					}
					if (hycableTray5 != null)
					{
						Connector closeConnector = hycableTray.GetCloseConnector(xyz2);
						Connector closeConnector2 = hycableTray2.GetCloseConnector(xyz2);
						Connector closeConnector3 = hycableTray3.GetCloseConnector(xyz2);
						Connector closeConnector4 = hycableTray5.GetCloseConnector(xyz2);
						Connector otherConnector = this.GetOtherConnector(hycableTray, closeConnector);
						XYZ vector = (closeConnector.Origin - otherConnector.Origin).Normalize();
						double z = otherConnector.Origin.Z;
						if (closeConnector2.Origin.Z < otherConnector.Origin.Z)
						{
							throw new Exception("请检查桥架位置是否正确。");
						}
						if (closeConnector == null || closeConnector.IsConnected || closeConnector2 == null || closeConnector2.IsConnected || closeConnector3 == null || closeConnector3.IsConnected || closeConnector4 == null || closeConnector4.IsConnected)
						{
							throw new Exception("桥架已经连接或桥架位置不正确。");
						}
						if (this.IsVerticalIntersection(hycableTray, hycableTray5) && hycableTray3.IsCableTrayOnLine(hycableTray5.CableTray) && this.IsSameWH(hycableTray2, hycableTray3) && this.IsSameWH(hycableTray3, hycableTray5) && this.IsSameW(hycableTray, hycableTray2))
						{
							CableTrayType cableTrayType = document.GetElement(hycableTray.CableTray.GetTypeId()) as CableTrayType;
							FamilySymbol fittingType = CableTrayFitingSel.GetFittingType(document, VerticalCableTrayFittingType.VerticalUpCornerCross, cableTrayType);
							if (fittingType == null)
							{
								throw new Exception("请先进行桥架设置。");
							}
							UnifiedModified.ActivateFamilySymbol(fittingType);
							FamilyInstance familyInstance = document.Create.NewFamilyInstance(xyz2, fittingType, 0);
							document.Regenerate();
							this.RotateFitting4(hycableTray2, closeConnector2, closeConnector4, familyInstance);
							if (!this.GetOtherConnector(hycableTray, closeConnector).IsConnected)
							{
								this.RotateTray3(hycableTray2, hycableTray, closeConnector, VerticalCableTrayFittingType.VerticalUpCornerCross, familyInstance);
							}
							XYZ lineDirection = this.GetLineDirection(document, hycableTray2, closeConnector2);
							XYZ lineDirection2 = this.GetLineDirection(document, hycableTray3, closeConnector3);
							XYZ lineDirection3 = this.GetLineDirection(document, hycableTray5, closeConnector4);
							foreach (Connector connector in RevitUtils.GetConduitFittingConnectors(familyInstance))
							{
								if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(lineDirection))
								{
									closeConnector2.ConnectTo(connector);
								}
								else if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(lineDirection2))
								{
									closeConnector3.ConnectTo(connector);
								}
								else if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(lineDirection3))
								{
									closeConnector4.ConnectTo(connector);
								}
								else
								{
									closeConnector.ConnectTo(connector);
								}
							}
							familyInstance.GetParameter("桥架宽度").Set(hycableTray2.CableTray.Width);
							familyInstance.GetParameter("桥架高度").Set(hycableTray2.CableTray.Height);
							familyInstance.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM).Set(hycableTray2.CableTray.get_Parameter((BuiltInParameter)(-1114132)).AsDouble());
							document.Regenerate();
							if (!otherConnector.IsConnected)
							{
								Connector connectorByVector = this.GetConnectorByVector(familyInstance, vector);
								XYZ origin = connectorByVector.Origin;
								Line curve = YJKLineEx.YJKGetBound(new XYZ(connectorByVector.Origin.X, connectorByVector.Origin.Y, z), origin);
								(verticalCableTray.CableTray.Location as LocationCurve).Curve = curve;
								document.Regenerate();
							}
						}
					}
				}
				IL_729:
				subTransaction.Commit();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
		}

		private List<HYCableTray<CableTray>> GetVerticalCableTrays(params HYCableTray<CableTray>[] myCustomCableTrays)
		{
			List<HYCableTray<CableTray>> list = new List<HYCableTray<CableTray>>();
			foreach (HYCableTray<CableTray> hycableTray in myCustomCableTrays)
			{
				if (Math.Abs(hycableTray.CustomConnectorStart.XYZ.Z - hycableTray.CustomConnectorEnd.XYZ.Z) >= 0.0001)
				{
					list.Add(hycableTray);
				}
			}
			return list;
		}

		private HYCableTray<CableTray> GetVerticalCableTray(params HYCableTray<CableTray>[] myCustomCableTrays)
		{
			foreach (HYCableTray<CableTray> hycableTray in myCustomCableTrays)
			{
				if (Math.Abs(hycableTray.CustomConnectorStart.XYZ.Z - hycableTray.CustomConnectorEnd.XYZ.Z) >= 0.0001)
				{
					return hycableTray;
				}
			}
			return myCustomCableTrays[0];
		}

		public bool NewPlanarConnection(IList<CableTray> cableTrays)
		{
			SubTransaction subTransaction = null;
			bool flag = false;
			bool result;
			try
			{
				if (cableTrays.Count < 2)
				{
					throw new System.ArgumentNullException("CableTray", "is null");
				}
				Document document = cableTrays[0].Document;
				HYCableTray<CableTray> hycableTray = new HYCableTray<CableTray>(cableTrays[0]);
				HYCableTray<CableTray> hycableTray2 = new HYCableTray<CableTray>(cableTrays[1]);
				IList<CableTray> list = new List<CableTray>();
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				int num = hycableTray.CheckPipingsPosition(hycableTray2.CableTray);
				if (num != 10)
				{
					if (num == 11)
					{
						if (this.NewYZWFitting(cableTrays))
						{
							flag = true;
						}
					}
				}
				else
				{
					bool flag2 = hycableTray.CustomConnectorStart.Connector.Width == hycableTray2.CustomConnectorStart.Connector.Width && hycableTray.CustomConnectorStart.Connector.Height == hycableTray2.CustomConnectorStart.Connector.Height;
					if (flag2)
					{
						if (hycableTray.Combine(hycableTray2.CableTray))
						{
							flag = true;
						}
					}
					else
					{
						list.Add(hycableTray.CableTray);
						list.Add(hycableTray2.CableTray);
						if (this.NewTransitionFitting(list) != null)
						{
							flag = true;
						}
					}
				}
				document.Regenerate();
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception)
			{
				flag = false;
				result = flag;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		private bool CableTraysConnect(IList<HYCableTray<CableTray>> orderCableTrays, Document doc)
		{
			bool result = false;
			if (orderCableTrays.Count > 1)
			{
				IList<CableTray> list = new List<CableTray>();
				for (int i = 0; i < orderCableTrays.Count; i += 2)
				{
					HYCableTray<CableTray> hycableTray;
					HYCableTray<CableTray> hycableTray2;
					if (orderCableTrays[i].CableTray.get_Parameter(BuiltInParameter.RBS_OFFSET_PARAM).AsDouble() > orderCableTrays[i + 1].CableTray.get_Parameter(BuiltInParameter.RBS_OFFSET_PARAM).AsDouble())
					{
						hycableTray = new HYCableTray<CableTray>(orderCableTrays[i].CableTray);
						hycableTray2 = new HYCableTray<CableTray>(orderCableTrays[i + 1].CableTray);
					}
					else
					{
						hycableTray = new HYCableTray<CableTray>(orderCableTrays[i + 1].CableTray);
						hycableTray2 = new HYCableTray<CableTray>(orderCableTrays[i].CableTray);
					}
					list.Clear();
					IList<HYCableTray<CableTray>> list2 = new List<HYCableTray<CableTray>>();
					list2.Add(hycableTray);
					list2.Add(hycableTray2);
					switch (hycableTray.CheckPipingsPosition(hycableTray2.CableTray))
					{
					case 10:
					{
						bool flag = hycableTray.CustomConnectorStart.Connector.Width == hycableTray2.CustomConnectorStart.Connector.Width && hycableTray.CustomConnectorStart.Connector.Height == hycableTray2.CustomConnectorStart.Connector.Height;
						if (flag)
						{
							hycableTray.Combine(hycableTray2.CableTray);
						}
						else
						{
							list.Add(hycableTray.CableTray);
							list.Add(hycableTray2.CableTray);
							this.NewTransitionFitting(list);
							doc.Regenerate();
						}
						break;
					}
					case 11:
					{
						bool flag2 = true;
						IList<XYZ> appAndEndPoint = hycableTray.GetAppAndEndPoint(hycableTray2.CableTray, ref flag2);
						if (appAndEndPoint != null && appAndEndPoint.Count > 0)
						{
							if (!flag2)
							{
								XYZ value = appAndEndPoint[0];
								appAndEndPoint[0] = appAndEndPoint[1];
								appAndEndPoint[1] = value;
							}
							if (hycableTray.IsPointOnCableTrayCurve(appAndEndPoint[1]) && !hycableTray.CustomConnectorStart.XYZ.IsAlmostEqualTo(appAndEndPoint[1]) && !hycableTray.CustomConnectorEnd.XYZ.IsAlmostEqualTo(appAndEndPoint[1]))
							{
								CableTray item = HYCableTrayConnect.NewCableTray(hycableTray2.CableTray, appAndEndPoint[0], appAndEndPoint[1]);
								list.Add(item);
								list.Add(hycableTray2.CableTray);
								this.NewElbowFitting(list);
								list.Clear();
								list.Add(item);
								list.Add(hycableTray.CableTray);
								list.Add(hycableTray.CableTray);
								this.NewTeeFitting(list);
							}
							else if (hycableTray2.IsPointOnCableTrayCurve(appAndEndPoint[1]) && !hycableTray2.CustomConnectorStart.XYZ.IsAlmostEqualTo(appAndEndPoint[1]) && !hycableTray2.CustomConnectorEnd.XYZ.IsAlmostEqualTo(appAndEndPoint[1]))
							{
								CableTray item = HYCableTrayConnect.NewCableTray(hycableTray.CableTray, appAndEndPoint[0], appAndEndPoint[1]);
								list.Add(item);
								list.Add(hycableTray.CableTray);
								this.NewElbowFitting(list);
								list.Clear();
								list.Add(item);
								list.Add(hycableTray2.CableTray);
								list.Add(hycableTray2.CableTray);
								this.NewTeeFitting(list);
							}
							else
							{
								CableTray item = HYCableTrayConnect.NewCableTray(hycableTray.CableTray, appAndEndPoint[0], appAndEndPoint[1]);
								list.Add(item);
								list.Add(hycableTray.CableTray);
								this.NewElbowFitting(list);
								list.Clear();
								list.Add(item);
								list.Add(hycableTray2.CableTray);
								this.NewElbowFitting(list);
							}
						}
						else
						{
							IList<Connector> appropriateConnector = hycableTray.GetAppropriateConnector(hycableTray2.CableTray);
							if (appropriateConnector != null && appropriateConnector.Count > 1)
							{
								XYZ point = (appropriateConnector[0].Origin + appropriateConnector[1].Origin) / 2.0;
								XYZ lineProject = hycableTray.GetLineProject(point);
								XYZ lineProject2 = hycableTray2.GetLineProject(point);
								HYCableTray<CableTray> hycableTray3 = new HYCableTray<CableTray>(HYCableTrayConnect.NewCableTray(hycableTray.CableTray, lineProject, lineProject2));
								list.Add(hycableTray.CableTray);
								list.Add(hycableTray3.CableTray);
								this.NewElbowFitting(list);
								doc.Regenerate();
								list.Clear();
								list.Add(hycableTray2.CableTray);
								list.Add(hycableTray3.CableTray);
								this.NewElbowFitting(list);
								doc.Regenerate();
							}
						}
						break;
					}
					case 12:
						list.Add(hycableTray.CableTray);
						list.Add(hycableTray2.CableTray);
						this.NewElbowFitting(list);
						break;
					case 13:
					{
						list.Add(hycableTray.CableTray);
						list.Add(hycableTray2.CableTray);
						CableTray pointOnCableTray = this.GetPointOnCableTray(list);
						if (pointOnCableTray != null)
						{
							list.Add(pointOnCableTray);
						}
						this.NewTeeFitting(list);
						break;
					}
					case 14:
						list.Add(hycableTray.CableTray);
						list.Add(hycableTray.CableTray);
						list.Add(hycableTray2.CableTray);
						list.Add(hycableTray2.CableTray);
						this.NewCrossFitting(list);
						break;
					case 21:
					{
						bool flag3 = true;
						IList<XYZ> appAndEndPoint2 = hycableTray.GetAppAndEndPoint(hycableTray2.CableTray, ref flag3);
						if (appAndEndPoint2 != null && appAndEndPoint2.Count > 0)
						{
							XYZ endXYZ = new XYZ(appAndEndPoint2[1].X, appAndEndPoint2[1].Y, appAndEndPoint2[0].Z);
							if (flag3)
							{
								CableTray item2 = HYCableTrayConnect.NewCableTray(hycableTray2.CableTray, appAndEndPoint2[0], endXYZ);
								CableTray item3 = HYCableTrayConnect.NewCableTray(hycableTray2.CableTray, appAndEndPoint2[1], endXYZ);
								list.Add(hycableTray.CableTray);
								list.Add(item2);
								this.NewElbowFitting(list);
								doc.Regenerate();
								list.Clear();
								list.Add(item2);
								list.Add(item3);
								this.NewElbowFitting(list);
								doc.Regenerate();
								list.Clear();
								if (this.IsCreateElbow(hycableTray2, appAndEndPoint2[1]))
								{
									list.Add(hycableTray2.CableTray);
									list.Add(item3);
									this.NewElbowFitting(list);
								}
								else
								{
									list.Add(hycableTray2.CableTray);
									list.Add(hycableTray2.CableTray);
									list.Add(item3);
									this.NewTeeFitting(list);
								}
							}
							else
							{
								CableTray item4 = HYCableTrayConnect.NewCableTray(hycableTray.CableTray, appAndEndPoint2[0], endXYZ);
								CableTray item5 = HYCableTrayConnect.NewCableTray(hycableTray.CableTray, appAndEndPoint2[1], endXYZ);
								list.Add(hycableTray2.CableTray);
								list.Add(item5);
								this.NewElbowFitting(list);
								doc.Regenerate();
								list.Clear();
								list.Add(item4);
								list.Add(item5);
								this.NewElbowFitting(list);
								doc.Regenerate();
								list.Clear();
								if (this.IsCreateElbow(hycableTray, appAndEndPoint2[0]))
								{
									list.Add(hycableTray.CableTray);
									list.Add(item4);
									this.NewElbowFitting(list);
								}
								else
								{
									list.Add(hycableTray.CableTray);
									list.Add(hycableTray.CableTray);
									list.Add(item4);
									this.NewTeeFitting(list);
								}
							}
						}
						else
						{
							IList<XYZ> point2 = this.GetPoint21(list2);
							if (point2 != null && point2.Count > 0)
							{
								CableTray item6 = HYCableTrayConnect.NewCableTray(hycableTray.CableTray, point2[0], point2[2]);
								CableTray item7 = HYCableTrayConnect.NewCableTray(hycableTray.CableTray, point2[1], point2[2]);
								if (hycableTray.IsPointOnCableTrayLine(point2[0]))
								{
									list.Add(hycableTray.CableTray);
									list.Add(item6);
									this.NewElbowFitting(list);
									doc.Regenerate();
									list.Clear();
									list.Add(item6);
									list.Add(item7);
									this.NewElbowFitting(list);
									doc.Regenerate();
									list.Clear();
									list.Add(item7);
									list.Add(hycableTray2.CableTray);
									this.NewElbowFitting(list);
									doc.Regenerate();
								}
							}
						}
						break;
					}
					case 22:
					{
						List<HYCableTray<CableTray>> newCableTray = this.GetNewCableTray(list2, doc);
						list.Add(hycableTray.CableTray);
						list.Add(newCableTray[0].CableTray);
						this.NewElbowFitting(list);
						list.Clear();
						list.Add(hycableTray2.CableTray);
						list.Add(newCableTray[1].CableTray);
						this.NewElbowFitting(list);
						break;
					}
					case 23:
					{
						List<HYCableTray<CableTray>> newCableTray = this.GetNewCableTray(list2, doc);
						if (hycableTray.IsPointOnCableTrayCurve(newCableTray[0].CustomConnectorStart.XYZ) || hycableTray.IsPointOnCableTrayCurve(newCableTray[0].CustomConnectorEnd.XYZ))
						{
							list.Add(hycableTray.CableTray);
							list.Add(hycableTray.CableTray);
							list.Add(newCableTray[0].CableTray);
							this.NewTeeFitting(list);
							list.Clear();
							list.Add(hycableTray2.CableTray);
							list.Add(newCableTray[1].CableTray);
							this.NewElbowFitting(list);
						}
						else
						{
							list.Add(hycableTray.CableTray);
							list.Add(newCableTray[0].CableTray);
							this.NewElbowFitting(list);
							list.Clear();
							list.Add(hycableTray2.CableTray);
							list.Add(hycableTray2.CableTray);
							list.Add(newCableTray[1].CableTray);
							this.NewTeeFitting(list);
						}
						break;
					}
					case 24:
					{
						List<HYCableTray<CableTray>> newCableTray = this.GetNewCableTray(list2, doc);
						list.Add(hycableTray.CableTray);
						list.Add(hycableTray.CableTray);
						list.Add(newCableTray[0].CableTray);
						this.NewTeeFitting(list);
						list.Clear();
						list.Add(hycableTray2.CableTray);
						list.Add(hycableTray2.CableTray);
						list.Add(newCableTray[1].CableTray);
						this.NewTeeFitting(list);
						break;
					}
					}
					result = true;
				}
				return result;
			}
			throw new Exception("没有找到适合的桥架匹配类型。");
		}

		private bool IsCreateElbow(HYCableTray<CableTray> ct, XYZ pt)
		{
			bool result = false;
			if (ct.EndXYZ.DistanceTo(pt) < 0.1 || ct.StartXYZ.DistanceTo(pt) < 0.1)
			{
				result = true;
			}
			return result;
		}

		private IList<XYZ> GetPoint21(IList<HYCableTray<CableTray>> hyCableTrays)
		{
			IList<XYZ> list = new List<XYZ>();
			IList<Connector> appropriateConnector = hyCableTrays[0].GetAppropriateConnector(hyCableTrays[1].CableTray);
			XYZ xyz = null;
			if (appropriateConnector != null && appropriateConnector.Count > 1)
			{
				if (!appropriateConnector[0].IsConnected && !appropriateConnector[1].IsConnected)
				{
					xyz = (appropriateConnector[0].Origin + appropriateConnector[1].Origin) / 2.0;
				}
				else if (appropriateConnector[0].IsConnected)
				{
					xyz = hyCableTrays[0].GetDistanceConnector(appropriateConnector[0].Origin).Origin;
				}
				else
				{
					xyz = hyCableTrays[1].GetDistanceConnector(appropriateConnector[1].Origin).Origin;
				}
			}
			if (xyz != null)
			{
				XYZ project = hyCableTrays[0].GetProject(xyz, true);
				XYZ project2 = hyCableTrays[1].GetProject(xyz, true);
				XYZ item = new XYZ(project2.X, project2.Y, project.Z);
				list.Add(project);
				list.Add(project2);
				list.Add(item);
			}
			return list;
		}

		private List<HYCableTray<CableTray>> GetNewCableTray(IList<HYCableTray<CableTray>> hyCableTrays, Document doc)
		{
			List<HYCableTray<CableTray>> list = new List<HYCableTray<CableTray>>();
			XYZ defferentPlaneInstersection = hyCableTrays[0].GetDefferentPlaneInstersection(hyCableTrays[1].CableTray);
			XYZ defferentPlaneInstersection2 = hyCableTrays[1].GetDefferentPlaneInstersection(hyCableTrays[0].CableTray);
			CableTray sourceCableTray = (hyCableTrays[0].GetDiameter() < hyCableTrays[1].GetDiameter()) ? hyCableTrays[0].CableTray : hyCableTrays[1].CableTray;
			Connector appropriateConnector = hyCableTrays[0].GetAppropriateConnector(defferentPlaneInstersection);
			Connector appropriateConnector2 = hyCableTrays[1].GetAppropriateConnector(defferentPlaneInstersection2);
			if (appropriateConnector.IsConnected && !hyCableTrays[0].IsPointOnCableTrayCurve(defferentPlaneInstersection))
			{
				Connector distanceConnector = hyCableTrays[0].GetDistanceConnector(defferentPlaneInstersection);
				XYZ xyz = (distanceConnector.Origin - appropriateConnector.Origin).Normalize();
				double num = distanceConnector.Origin.DistanceTo(defferentPlaneInstersection) + 0.3;
				XYZ startXYZ = defferentPlaneInstersection + xyz * num;
				XYZ endXYZ = defferentPlaneInstersection2 + xyz * num;
				CableTray cableTray = HYCableTrayConnect.NewCableTray(sourceCableTray, startXYZ, endXYZ);
				CableTray cableTray2 = HYCableTrayConnect.NewCableTray(sourceCableTray, defferentPlaneInstersection2, endXYZ);
				this.NewElbowFitting(new List<CableTray>
				{
					cableTray,
					cableTray2
				});
				list.Add(new HYCableTray<CableTray>(cableTray));
				list.Add(new HYCableTray<CableTray>(cableTray2));
			}
			else if (appropriateConnector2.IsConnected && !hyCableTrays[1].IsPointOnCableTrayCurve(defferentPlaneInstersection2))
			{
				Connector distanceConnector2 = hyCableTrays[1].GetDistanceConnector(defferentPlaneInstersection2);
				XYZ xyz2 = (distanceConnector2.Origin - appropriateConnector2.Origin).Normalize();
				double num2 = distanceConnector2.Origin.DistanceTo(defferentPlaneInstersection2) + 0.3;
				XYZ xyz3 = defferentPlaneInstersection + xyz2 * num2;
				XYZ endXYZ2 = defferentPlaneInstersection2 + xyz2 * num2;
				CableTray cableTray3 = HYCableTrayConnect.NewCableTray(sourceCableTray, xyz3, endXYZ2);
				CableTray cableTray4 = HYCableTrayConnect.NewCableTray(sourceCableTray, defferentPlaneInstersection, xyz3);
				this.NewElbowFitting(new List<CableTray>
				{
					cableTray3,
					cableTray4
				});
				list.Add(new HYCableTray<CableTray>(cableTray4));
				list.Add(new HYCableTray<CableTray>(cableTray3));
			}
			else
			{
				HYCableTray<CableTray> item = new HYCableTray<CableTray>(HYCableTrayConnect.NewCableTray(sourceCableTray, defferentPlaneInstersection, defferentPlaneInstersection2));
				list.Add(item);
				list.Add(item);
			}
			return list;
		}

		public bool NewSomeConnect(IList<CableTray> cableTrays)
		{
			bool flag = true;
			bool result;
			try
			{
				if (cableTrays == null || cableTrays.Count < 2)
				{
					throw new Exception("最少选择2根桥架");
				}
				Document document = cableTrays[0].Document;
				IList<HYCableTray<CableTray>> list = new List<HYCableTray<CableTray>>();
				list = this.SomeCableTrayOrderBy(cableTrays);
				if (list != null && list.Count > 1)
				{
					flag = this.CableTraysConnect(list, document);
				}
				result = flag;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		private IList<HYCableTray<CableTray>> SomeCableTrayOrderBy(IList<CableTray> cableTrays)
		{
			IList<HYCableTray<CableTray>> list = new List<HYCableTray<CableTray>>();
			switch (cableTrays.Count)
			{
			case 1:
				throw new Exception("请选择多根桥架");
			case 2:
			{
				HYCableTray<CableTray> hycableTray = new HYCableTray<CableTray>(cableTrays[0]);
				HYCableTray<CableTray> hycableTray2 = new HYCableTray<CableTray>(cableTrays[1]);
				if (hycableTray.IsPipingOnLine(hycableTray2.CableTray))
				{
					IList<CableTray> list2 = new List<CableTray>();
					if (this.IsSameWH(hycableTray, hycableTray2))
					{
						hycableTray.Combine(hycableTray2.CableTray);
					}
					else
					{
						list2.Add(hycableTray.CableTray);
						list2.Add(hycableTray2.CableTray);
						this.NewTransitionFitting(list2);
					}
				}
				else
				{
					list.Add(hycableTray);
					list.Add(hycableTray2);
				}
				break;
			}
			case 3:
			{
				HYCableTray<CableTray> hyCableTray = new HYCableTray<CableTray>(cableTrays[0]);
				HYCableTray<CableTray> hyCableTray2 = new HYCableTray<CableTray>(cableTrays[1]);
				HYCableTray<CableTray> hyCableTray3 = new HYCableTray<CableTray>(cableTrays[2]);
				list = this.OrderThreeCableTray(hyCableTray, hyCableTray2, hyCableTray3);
				break;
			}
			case 4:
			{
				IList<CableTray> list3 = new List<CableTray>();
				int num = 0;
				HYCableTray<CableTray> hycableTray3 = new HYCableTray<CableTray>(cableTrays[0]);
				HYCableTray<CableTray> hycableTray4 = new HYCableTray<CableTray>(cableTrays[1]);
				HYCableTray<CableTray> hycableTray5 = new HYCableTray<CableTray>(cableTrays[2]);
				HYCableTray<CableTray> hycableTray6 = new HYCableTray<CableTray>(cableTrays[3]);
				if (hycableTray3.IsPipingOnLine(hycableTray4.CableTray))
				{
					if (this.IsSameWH(hycableTray3, hycableTray4))
					{
						hycableTray3.Combine(hycableTray4.CableTray);
						num++;
					}
					if (this.IsSameWH(hycableTray5, hycableTray6))
					{
						hycableTray5.Combine(hycableTray6.CableTray);
						num += 2;
					}
					if (num == 0)
					{
						try
						{
							this.NewCrossFitting(cableTrays);
							break;
						}
						catch (Exception)
						{
							break;
						}
					}
					if (1 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray5.CableTray))
						{
							throw new Exception("请检查桥架是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray5.CableTray);
						list3.Add(hycableTray6.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (2 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray5.CableTray))
						{
							throw new Exception("请检查桥架是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray5.CableTray);
						list3.Add(hycableTray5.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (3 == num)
					{
						list.Add(hycableTray3);
						list.Add(hycableTray5);
					}
				}
				else if (hycableTray3.IsPipingOnLine(hycableTray5.CableTray))
				{
					if (this.IsSameWH(hycableTray3, hycableTray5))
					{
						hycableTray3.Combine(hycableTray5.CableTray);
						num++;
					}
					if (this.IsSameWH(hycableTray4, hycableTray6))
					{
						hycableTray4.Combine(hycableTray6.CableTray);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(cableTrays);
					}
					else if (1 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray4.CableTray))
						{
							throw new Exception("请检查桥架是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray6.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (2 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray4.CableTray))
						{
							throw new Exception("请检查桥架是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray5.CableTray);
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray4.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (3 == num)
					{
						list.Add(hycableTray3);
						list.Add(hycableTray4);
					}
				}
				else if (hycableTray3.IsPipingOnLine(hycableTray6.CableTray))
				{
					if (this.IsSameWH(hycableTray3, hycableTray6))
					{
						hycableTray3.Combine(hycableTray6.CableTray);
						num++;
					}
					if (this.IsSameWH(hycableTray4, hycableTray5))
					{
						hycableTray4.Combine(hycableTray5.CableTray);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(cableTrays);
					}
					else if (1 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray4.CableTray))
						{
							throw new Exception("请检查桥架是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray5.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (2 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray4.CableTray))
						{
							throw new Exception("请检查桥架是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray5.CableTray);
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray4.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (3 == num)
					{
						list.Add(hycableTray3);
						list.Add(hycableTray4);
					}
				}
				else if (hycableTray4.IsPipingOnLine(hycableTray5.CableTray))
				{
					if (this.IsSameWH(hycableTray4, hycableTray5))
					{
						hycableTray4.Combine(hycableTray5.CableTray);
						num++;
					}
					if (this.IsSameWH(hycableTray3, hycableTray6))
					{
						hycableTray3.Combine(hycableTray6.CableTray);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(cableTrays);
					}
					else if (1 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray4.CableTray))
						{
							throw new Exception("请检查桥架是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray6.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (2 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray4.CableTray))
						{
							throw new Exception("请检查桥架是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray5.CableTray);
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray3.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (3 == num)
					{
						list.Add(hycableTray3);
						list.Add(hycableTray4);
					}
				}
				else if (hycableTray4.IsPipingOnLine(hycableTray6.CableTray))
				{
					if (this.IsSameWH(hycableTray4, hycableTray6))
					{
						hycableTray4.Combine(hycableTray6.CableTray);
						num++;
					}
					if (this.IsSameWH(hycableTray3, hycableTray5))
					{
						hycableTray3.Combine(hycableTray5.CableTray);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(cableTrays);
					}
					else if (1 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray4.CableTray))
						{
							throw new Exception("请检查桥架是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray5.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (2 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray4.CableTray))
						{
							throw new Exception("请检查桥架是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray6.CableTray);
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray3.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (3 == num)
					{
						list.Add(hycableTray3);
						list.Add(hycableTray4);
					}
				}
				else if (hycableTray5.IsPipingOnLine(hycableTray6.CableTray))
				{
					if (this.IsSameWH(hycableTray5, hycableTray6))
					{
						hycableTray5.Combine(hycableTray6.CableTray);
						num++;
					}
					if (this.IsSameWH(hycableTray3, hycableTray4))
					{
						hycableTray3.Combine(hycableTray4.CableTray);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(cableTrays);
					}
					else if (1 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray5.CableTray))
						{
							throw new Exception("请检查桥架是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray5.CableTray);
						list3.Add(hycableTray5.CableTray);
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray4.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (2 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray5.CableTray))
						{
							throw new Exception("请检查桥架是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray5.CableTray);
						list3.Add(hycableTray6.CableTray);
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray3.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (3 == num)
					{
						list.Add(hycableTray3);
						list.Add(hycableTray5);
					}
				}
				else if (hycableTray3.IsIntersection(hycableTray4.CableTray))
				{
					list.Add(hycableTray3);
					list.Add(hycableTray4);
				}
				else if (hycableTray3.IsIntersection(hycableTray5.CableTray))
				{
					list.Add(hycableTray3);
					list.Add(hycableTray5);
				}
				else if (hycableTray3.IsIntersection(hycableTray6.CableTray))
				{
					list.Add(hycableTray3);
					list.Add(hycableTray6);
				}
				else if (hycableTray4.IsIntersection(hycableTray5.CableTray))
				{
					list.Add(hycableTray4);
					list.Add(hycableTray5);
				}
				else if (hycableTray4.IsIntersection(hycableTray6.CableTray))
				{
					list.Add(hycableTray4);
					list.Add(hycableTray6);
				}
				else if (hycableTray5.IsIntersection(hycableTray6.CableTray))
				{
					list.Add(hycableTray5);
					list.Add(hycableTray6);
				}
				else
				{
					list.Add(hycableTray3);
					list.Add(hycableTray4);
				}
				break;
			}
			default:
				throw new Exception("桥架不能超过四根");
			}
			return list;
		}

		private CableTray GetPointOnCableTray(IList<CableTray> hyCableTrays)
		{
			CableTray result = null;
			HYCableTray<CableTray> hycableTray = new HYCableTray<CableTray>(hyCableTrays[0]);
			HYCableTray<CableTray> hycableTray2 = new HYCableTray<CableTray>(hyCableTrays[1]);
			if (hyCableTrays.Count == 2)
			{
				XYZ xyz = hycableTray.IntersectionPoint(hycableTray2.CableTray);
				if (xyz != null)
				{
					if (hycableTray.IsPointOnCableTrayCurve(xyz))
					{
						result = hycableTray.CableTray;
					}
					else if (hycableTray2.IsPointOnCableTrayCurve(xyz))
					{
						result = hycableTray2.CableTray;
					}
				}
			}
			return result;
		}

		private bool IsSameWH(HYCableTray<CableTray> hyCableTray1, HYCableTray<CableTray> hyCableTray2)
		{
			return Math.Abs(hyCableTray2.CustomConnectorStart.Connector.Width - hyCableTray1.CustomConnectorStart.Connector.Width) < 0.0001 && Math.Abs(hyCableTray2.CustomConnectorStart.Connector.Height - hyCableTray1.CustomConnectorStart.Connector.Height) < 0.0001;
		}

		private IList<HYCableTray<CableTray>> OrderThreeCableTray(HYCableTray<CableTray> hyCableTray, HYCableTray<CableTray> hyCableTray1, HYCableTray<CableTray> hyCableTray2)
		{
			IList<HYCableTray<CableTray>> list = new List<HYCableTray<CableTray>>();
			IList<CableTray> list2 = new List<CableTray>();
			list2.Add(hyCableTray.CableTray);
			list2.Add(hyCableTray1.CableTray);
			list2.Add(hyCableTray2.CableTray);
			if (hyCableTray.IsPipingOnLine(hyCableTray1.CableTray))
			{
				if (this.IsSameWH(hyCableTray, hyCableTray1))
				{
					hyCableTray.Combine(hyCableTray1.CableTray);
					list.Add(hyCableTray);
					list.Add(hyCableTray2);
				}
				else
				{
					try
					{
						this.NewTeeFitting(list2);
					}
					catch (Exception)
					{
					}
					list = null;
				}
			}
			else if (hyCableTray.IsPipingOnLine(hyCableTray2.CableTray))
			{
				if (this.IsSameWH(hyCableTray, hyCableTray2))
				{
					hyCableTray.Combine(hyCableTray2.CableTray);
					list.Add(hyCableTray);
					list.Add(hyCableTray1);
				}
				else
				{
					try
					{
						this.NewTeeFitting(list2);
					}
					catch (Exception)
					{
					}
					list = null;
				}
			}
			else if (hyCableTray1.IsPipingOnLine(hyCableTray2.CableTray))
			{
				if (this.IsSameWH(hyCableTray1, hyCableTray2))
				{
					hyCableTray1.Combine(hyCableTray2.CableTray);
					list.Add(hyCableTray);
					list.Add(hyCableTray1);
				}
				else
				{
					try
					{
						this.NewTeeFitting(list2);
					}
					catch (Exception)
					{
					}
					list = null;
				}
			}
			else if (hyCableTray.IsIntersection(hyCableTray1.CableTray))
			{
				list.Add(hyCableTray);
				list.Add(hyCableTray1);
			}
			else if (hyCableTray.IsIntersection(hyCableTray2.CableTray))
			{
				list.Add(hyCableTray);
				list.Add(hyCableTray2);
			}
			else if (hyCableTray1.IsIntersection(hyCableTray2.CableTray))
			{
				list.Add(hyCableTray2);
				list.Add(hyCableTray1);
			}
			else
			{
				list.Add(hyCableTray);
				list.Add(hyCableTray1);
			}
			return list;
		}

		public bool NewYZWFitting(IList<CableTray> cableTrays)
		{
			CableTray cableTray = null;
			Document document = null;
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (cableTrays.Count < 2)
				{
					throw new System.ArgumentNullException("CableTray", "is null");
				}
				document = cableTrays[0].Document;
				HYCableTray<CableTray> hycableTray = new HYCableTray<CableTray>(cableTrays[0]);
				HYCableTray<CableTray> hycableTray2 = new HYCableTray<CableTray>(cableTrays[1]);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				IList<Connector> appropriateConnector = hycableTray.GetAppropriateConnector(hycableTray2.CableTray);
				if (appropriateConnector == null || appropriateConnector.Count <= 1)
				{
					throw new Exception("请检查桥架的位置关系能否构成乙字弯");
				}
				cableTray = HYCableTrayConnect.NewCableTray(hycableTray.CableTray, appropriateConnector[0].Origin, appropriateConnector[1].Origin);
				HYCableTray<CableTray> hycableTray3 = new HYCableTray<CableTray>(cableTray);
				try
				{
					IList<CableTray> list = new List<CableTray>();
					list.Add(hycableTray.CableTray);
					list.Add(hycableTray3.CableTray);
					this.NewElbowFitting(list);
					list.Clear();
					list.Add(hycableTray2.CableTray);
					list.Add(hycableTray3.CableTray);
					this.NewElbowFitting(list);
				}
				catch (Exception)
				{
					document.DeleteElement(cableTray);
				}
				bool flag = true;
				document.Regenerate();
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception)
			{
				result = false;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public static CableTray NewCableTray(CableTray sourceCableTray, XYZ startXYZ, XYZ endXYZ)
		{
			CableTray cableTray = null;
			if (sourceCableTray == null || startXYZ == null || endXYZ == null)
			{
				return null;
			}
			Document document = sourceCableTray.Document;
			foreach (ElementId elementId in ElementTransformUtils.CopyElement(document, sourceCableTray.Id, new XYZ(0.0, 0.0, 0.0)))
			{
				cableTray = (document.GetElement(elementId) as CableTray);
			}
			document.Regenerate();
			if (cableTray != null)
			{
				Line curve = Line.CreateBound(startXYZ, endXYZ);
				(cableTray.Location as LocationCurve).Curve = curve;
			}
			document.Regenerate();
			return cableTray;
		}

		public FamilyInstance NewTransitionFitting(IList<CableTray> cableTrays)
		{
			FamilyInstance familyInstance = null;
			FamilyInstance result;
			try
			{
				if (cableTrays.Count < 2)
				{
					throw new System.ArgumentNullException("CableTray", "is null");
				}
				Document document = cableTrays[0].Document;
				HYCableTray<CableTray> hycableTray = new HYCableTray<CableTray>(cableTrays[0]);
				IList<Connector> list = new List<Connector>();
				list = hycableTray.GetAppropriateConnector(cableTrays[1]);
				if (list.Count > 1 && list[0] != null && list[1] != null && !list[0].IsConnected && !list[1].IsConnected)
				{
					familyInstance = document.Create.NewTransitionFitting(list[0], list[1]);
				}
				document.Regenerate();
				result = familyInstance;
			}
			catch (Exception)
			{
				familyInstance = null;
				result = familyInstance;
			}
			return result;
		}

		private IList<CableTray> OrderCableTrayPosition(IList<CableTray> cableTrays)
		{
			IList<CableTray> list = new List<CableTray>();
			new List<XYZ>();
			HYCableTray<CableTray> hycableTray = new HYCableTray<CableTray>(cableTrays[0]);
			HYCableTray<CableTray> hycableTray2 = new HYCableTray<CableTray>(cableTrays[1]);
			HYCableTray<CableTray> hycableTray3 = new HYCableTray<CableTray>(cableTrays[2]);
			if (3 == cableTrays.Count)
			{
				if (hycableTray.IsCableTrayOnLine(hycableTray2.CableTray) || hycableTray.IsParallel(hycableTray2.CableTray))
				{
					list.Add(hycableTray3.CableTray);
					list.Add(hycableTray.CableTray);
					list.Add(hycableTray2.CableTray);
				}
				else if (hycableTray.IsCableTrayOnLine(hycableTray3.CableTray) || hycableTray.IsCableTrayOnLine(hycableTray3.CableTray))
				{
					list.Add(hycableTray2.CableTray);
					list.Add(hycableTray.CableTray);
					list.Add(hycableTray3.CableTray);
				}
				else
				{
					list = cableTrays;
				}
			}
			else if (4 == cableTrays.Count)
			{
				HYCableTray<CableTray> hycableTray4 = new HYCableTray<CableTray>(cableTrays[3]);
				if (hycableTray.IsCableTrayOnLine(hycableTray2.CableTray))
				{
					list = cableTrays;
				}
				else if (hycableTray.IsCableTrayOnLine(hycableTray3.CableTray))
				{
					list.Add(hycableTray.CableTray);
					list.Add(hycableTray3.CableTray);
					list.Add(hycableTray2.CableTray);
					list.Add(hycableTray4.CableTray);
				}
				else if (hycableTray.IsCableTrayOnLine(hycableTray4.CableTray))
				{
					list.Add(hycableTray.CableTray);
					list.Add(hycableTray4.CableTray);
					list.Add(hycableTray2.CableTray);
					list.Add(hycableTray3.CableTray);
				}
				else
				{
					list = cableTrays;
				}
			}
			return list;
		}
	}
}
